home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / dev / cross / ava-0.2.5.lha / ava-0.2.5 / src / Preproc.h < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-23  |  4.1 KB  |  161 lines

  1. /* 
  2.   Preproc.h
  3.   Uros Platise, July 1998
  4. */
  5.  
  6. #ifndef __Preproc
  7. #define __Preproc
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <vector>
  12. #include <string>
  13. #include "Global.h"
  14. #include "Segment.h"
  15. #include "string.h"
  16.  
  17. #define PPC_MAXFILELEN    256
  18. #define PPC_TMPDIR    "/tmp/"
  19.  
  20. class TSource{
  21. private:
  22.   int CRef;        /* Smart Pointer Support */
  23. public:  
  24.   char name [PPC_MAXFILELEN];
  25.   long currPos;
  26.   long lineNumber;
  27.   PSegTable segP;
  28.   
  29. public:
  30.   virtual char *getstr(char *s, int size)=0;
  31.   virtual int putstr(const char *s)=0;
  32.   
  33.   virtual char getch()=0;    /* return zero on end-of-file */
  34.   virtual void putbackch()=0;    /* 1 character deep stack must be provided! */
  35.   
  36.   virtual void rew()=0;
  37.   virtual FILE* stream()=0;
  38.   virtual void seek(long offset, long new_ln)=0;
  39.   virtual ~TSource(){}
  40.   TSource():CRef(0),currPos(0),lineNumber(1){}
  41.   friend TPt<TSource>;
  42. };
  43. typedef TPt<TSource> PSource;
  44.  
  45. class TPreproc {
  46. private:
  47.   TMicroStack<PSource> srcList;
  48.   vector<string> dirList;
  49.   typedef vector<string>::const_iterator TstrCI;
  50.  
  51.   long markPos;        /* mark cursor position for error reporting */
  52.   long markLine;
  53.   char oldSourceName[PPC_MAXFILELEN];
  54.   char extSourceName[PPC_MAXFILELEN];
  55.   bool extMarker;
  56.   int fileN;        /* number of open files -> linker, skipping files */
  57.   
  58. public:
  59.   PSource csrc;        /* current source */
  60.   
  61. public:
  62.   TPreproc():markPos(1),markLine(1),extMarker(false),fileN(0){
  63.     oldSourceName[0]=0;}
  64.   ~TPreproc(){}
  65.  
  66.   void insert(PSource srcP, bool copyParentInfo=true);
  67.   void insert(char* fullName);
  68.   
  69.   void AddDir();    /* Adds directory list */
  70.   void AddDir(const char* directory);
  71.   char* FindFullPathName(char* filename_buf);    /* returns to filename_buf */
  72.   
  73.   bool next();        /* is true, when next file is found, otherwise false.
  74.                            If next file is not found, first one is set again */
  75.   void seek(long offset, long new_nl){if(csrc()!=NULL){
  76.     csrc->seek(offset,new_nl);}}
  77.                               
  78.   /* character retrive funcs. */
  79.                
  80.   char* getstr(char *s, int size);
  81.   char getch();
  82.   void putbackch(){if(csrc()!=NULL){csrc->putbackch();}}
  83.   
  84.   /* report functions */
  85.   
  86.   void mark(){
  87.     extMarker=false; 
  88.     if (csrc()!=NULL){markPos=csrc->currPos;markLine=csrc->lineNumber;}
  89.   }
  90.   void mark(int lineNo, const char* s){
  91.     extMarker=true; markLine=lineNo; strcpy(extSourceName,s);}
  92.   const char* name(){
  93.     if (extMarker==true){return extSourceName;}
  94.     return (csrc()!=NULL)?csrc->name:oldSourceName;
  95.   }
  96.   long line(){return markLine;}
  97.   long curpos(){return markPos;}
  98.   long lxline(){return (csrc()!=NULL)?csrc->lineNumber:markLine;}
  99.   long lxcur(){return (csrc()!=NULL)?csrc->currPos:markPos;}  
  100.   
  101.   bool firstSource(){return srcList.size()<2;}  
  102. };
  103.  
  104. extern TPreproc preproc;
  105.  
  106. /* FILE I/O Interface Support */
  107. class TFile: public TSource{
  108. private:
  109.   bool temporary;
  110.   FILE *fd;
  111.   bool bufferCh;
  112.   char ch;    /* set ch=0 to avoid buffering before first ch is read */
  113. public:
  114.   TFile();
  115.   TFile(const char* _fullName, const char* _mode="rt", bool temporary=false);
  116.   void reopen(const char* _fullName, const char* _mode="rt",
  117.               bool temporary=false);
  118.   ~TFile();
  119.  
  120.   char* getstr(char *s, int size){return fgets (s,size,fd);}
  121.   int putstr(const char *s) {return fputs (s,fd);}
  122.   
  123.   char getch();
  124.   void putbackch(){bufferCh=true;}
  125.   
  126.   void rew(){rewind(fd);}
  127.   void seek(long offset, long new_ln){
  128.     currPos=0; lineNumber=new_ln; fseek(fd, offset, SEEK_SET);}
  129.   inline FILE* stream(){return fd;}
  130. };
  131.  
  132. /* Memory Block I/O Interface Support */
  133. #define MEMBLOCK_LEN    4096
  134.  
  135. class TMemBlock: public TSource{
  136. private:
  137.   char mem[MEMBLOCK_LEN];
  138.   char ch;    /* set ch=0 to avoid buffering before first ch is read */
  139.   int idx,len;
  140.   bool bufferCh;
  141. public:
  142.   TMemBlock():ch(0),idx(0),len(0),bufferCh(false){mem[0]=0;}
  143.   ~TMemBlock(){}
  144.  
  145.   char* getstr (char *s, int size);
  146.   int putstr (const char *s){fprintf(stderr,"putstr(%s)\n",s);assert(0);}
  147.   
  148.   char getch();
  149.   void putbackch(){bufferCh=true;}
  150.   
  151.   void rew(){assert(0);}
  152.   void seek(long offset, long new_ln){
  153.     printf("TMemBlock: Cannot seek to: %ld @ %ld",offset,new_ln);assert(0);}
  154.   FILE* stream(){assert(0);}  
  155.   
  156.   void append (const char* s);
  157. };
  158.  
  159. #endif
  160.  
  161.